module;
#include "pch.h"
export module S306;

import DND;

import Panel.Main;
import Panel.Game;
import PanelRegime;
import World;
import Database;
import Regime;

export class S306 : public Game
{
public:
	enum class State
	{
		MAIN,//！主菜单
		GAME,//！游戏与编辑器
	};

	virtual void Init()
	{
		g_font->LoadFont("data/font/1.ttf");
		g_font->LoadFont("data/font/2.ttf");
		g_font->LoadFont("data/font/3.ttf");

		//配置文件目录
		g_file->CreateDirectory("data/bin/config/language/");
		g_file->CreateDirectory("data/bin/config/color/");

		g_factory->LoadSpriteSheetAll("data/image/", "data/bin/");

		
		if (true)
		{
			//语言文件转换编码
			g_file->ConvertMb2U8("data/config/language/normal.csv", "data/bin/config/language/normal.csv");
			g_file->ConvertMb2U8("data/config/language/location.csv", "data/bin/config/language/location.csv");
			g_file->ConvertMb2U8("data/config/language/area.csv", "data/bin/config/language/area.csv");
			//序列化颜色
			g_file->SerializeCsv("data/config/color/terrain.csv", "data/bin/config/color/terrain.dat");
			g_file->SerializeCsv("data/config/color/area.csv", "data/bin/config/color/area.dat");
		}
		
		//加载语言文件
		g_lang->LoadCsv("data/bin/config/language/normal.csv");
		g_lang->LoadCsv("data/bin/config/language/location.csv");
		g_lang->LoadCsv("data/bin/config/language/area.csv");

		//加载颜色文件
		g_factory->LoadColorCsv("data/bin/config/color/terrain.dat");
		g_factory->LoadColorCsv("data/bin/config/color/area.dat");

		g_db = new Database;

		_debugTool = new DebugTool;
		_debugTool->_fps.GetText()->SetFontId(1);


		_panelMain = new PanelMain;
		_panelGame = new PanelGame;
		_panelRegime = new PanelRegime();

		g_world = new World;
		g_world->Init();

		_state = State::MAIN;
	}
	virtual	void Run(const LogicType& lt)
	{

		//update 
		if(lt.IsUpdate()) 
		{
			switch (_state)
			{
			case State::MAIN:
			{
				_panelMain->Run(LogicType::UPDATE);

				size_t i = _panelMain->GetIndexMenu();
				if (i == 0)
				{
					_state = State::GAME;
				}
			}
				break;
			case State::GAME:
			{
				_panelGame->Run(LogicType::UPDATE);
				g_world->Run(LogicType::UPDATE, pointer(_panelGame->GetMedium()));

				//更新界面 data -> ui
				_panelGame->RefreshNode(g_world->GetMouseNode());
				_panelGame->RefreshFacility(g_world->GetSelectFacility(), true);
				_panelGame->RefreshAge(g_world->GetAge(), true);
				_panelGame->RefreshArea(g_world->GetSelectArea(), true);

				//手动执行的界面
				_panelGame->RunHand(LogicType::UPDATE);

				//回写数据 ui -> data
				_panelGame->RefreshFacility(g_world->GetSelectFacility(), false);
				_panelGame->RefreshAge(g_world->GetAge(), false);
				_panelGame->RefreshArea(g_world->GetSelectArea(), false);

				//势力界面
				if (_panelRegime->IsOpen())
					_panelRegime->Run(LogicType::UPDATE);

			}
				break;
			default:
				break;
			}
			

			
		}

		if (lt.IsRender())
		{
			switch (_state)
			{
			case State::MAIN:
			{
				_panelMain->Run(LogicType::RENDER);
			}
				break;
			case State::GAME:
			{
				_panelGame->Run(LogicType::RENDER);
				g_world->Run(LogicType::RENDER, pointer(_panelGame->GetMedium()));

				//手动 选择执行逻辑的项目
				_panelGame->RunHand(LogicType::RENDER);

				//势力界面
				if (_panelRegime->IsOpen())
					_panelRegime->Run(LogicType::RENDER);

				if (g_input->KeyState(KeyCode::CTRL))
				{
					_panelGame->RenderDebug();
					if (_panelRegime->IsOpen())
						_panelRegime->RenderDebug();
				}
			}
				break;
			default:
				break;
			}
		}

		_debugTool->Run(lt, this);

	}

	//打开 势力界面
	void OpenPanelRegime(Regime* regime)
	{
		_panelRegime->Refresh(regime);
		_panelRegime->SetOpen(true);
	}

	virtual	void Release()
	{
		delete g_db;

		delete _debugTool;

		delete _panelMain;
		delete _panelGame;

		delete g_world;
	}
private:
	struct DebugTool
	{
		void Run(const LogicType& lt, S306* s306)
		{
			if (lt.IsRender())
			{
				_fps.Render(format("缩放系数：{}\n", g_world->GetScale()));
			}
			_ruler.Run(lt);
		}
		Tool::ShowFps _fps;
		Tool::Ruler _ruler;
	};
	DebugTool* _debugTool;

	PanelMain* _panelMain;
	PanelGame* _panelGame;
	PanelRegime* _panelRegime;

	State _state;
};


export S306* g_s306;

